﻿Imports System.Drawing.Printing
Imports DevExpress.XtraPrinting

Public Class frmHistorialFacturas

    Private Sub frmHistorialFacturas_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TipospagoTableAdapter.Fill(DbFireAndIceDataSet.tipospago)
        ItemventaTableAdapter.Fill(DbFireAndIceDataSet.itemventa)
    End Sub

    Private Sub btBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btBuscar.Click
        frmBuscarFactura.ShowDialog()
    End Sub

    Private Sub TextEdit1_EditValueChanged(sender As System.Object, e As System.EventArgs) Handles TextEdit1.EditValueChanged
        Try
            IdTextBox.Text = TextEdit1.EditValue
            VfacturacompletaTableAdapter.FillByIdFact(DbFireAndIceDataSet.vfacturacompleta, TextEdit1.EditValue)
            FacturasdetalleTableAdapter.FillByIdFact(DbFireAndIceDataSet.facturasdetalle, TextEdit1.EditValue)
            FacturaspagoTableAdapter.FillByIdFact(DbFireAndIceDataSet.facturaspago, TextEdit1.EditValue)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No se cargaron los datos correctamente.")
            TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, _
                                              My.Resources.StringGlobal.prgAngely, "Sentencias para cargar los datos de la factura en el historial", _
                                              ex.Message, My.Resources.StringGlobal.mdlFacturacion, Date.Today, TimeOfDay.TimeOfDay.ToString, _
                                              My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
        End Try
        
    End Sub

    Private Sub btAnular_Click(sender As System.Object, e As System.EventArgs) Handles btAnular.Click
        Try
            Dim opcion As Integer
            opcion = MsgBox("¿Está seguro que desea anular la factura? Considere que este cambio es irreversible.", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Anular factura")
            If opcion = vbYes Then
                Try
                    FacturasTableAdapter1.UpdateQueryEstadoFactura(True, Val(IdTextBox.Text))
                    AlertControl1.AppearanceCaption.ForeColor = Color.Green
                    AlertControl1.Show(Me, "Notificación", "Se anuló la factura")
                Catch ex As Exception
                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                    AlertControl1.Show(Me, "Error", "No se pudo anular la factura.")
                    TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, _
                                                      My.Resources.StringGlobal.prgAngely, "Sentencia para anular la factura en la pantalla de Historial Facturas", _
                                                      ex.Message, My.Resources.StringGlobal.mdlFacturacion, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
                End Try
            End If
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No se pudo anular la factura.")
            TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, My.Resources.StringGlobal.prgAngely, "Sentencia para anular la factura en la pantalla de Historial Facturas", ex.Message, My.Resources.StringGlobal.mdlFacturacion, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
        End Try
    End Sub

    Private Sub btImprimir_Click(sender As System.Object, e As System.EventArgs) Handles btImprimir.Click
        Try
            'llamar el reporte
            If IdTextBox.Text = Nothing Then
                MsgBox("Lo datos de la factura no se cargaron completamente, no puede reimprimir este ticket", MsgBoxStyle.Information, "Notificación")
            Else
                If SubsidiootorgadoTextEdit.EditValue > 0 Then
                    Dim report As New rptFacturaReimpresa

                    report.VreportefacturaimpresaTableAdapter.FillByIdFact(report.DbFireAndIceDataSet1.vreportefacturaimpresa, Val(IdTextBox.Text))
                    report.VfacturaspagoTableAdapter.FillByIdFact(report.DbFireAndIceDataSet1.vfacturaspago, Val(IdTextBox.Text))
                    report.VfacturadetalleTableAdapter.FillByIdFact(report.DbFireAndIceDataSet1.vfacturadetalle, Val(IdTextBox.Text))
                    report.SysconfigTableAdapter1.FillById(report.DbFireAndIceDataSet1.sysconfig, 1)

                    Dim items As Integer = report.VfacturadetalleTableAdapter.ScalarQueryNumeroItemsPorFactura(Val(IdTextBox.Text))
                    Dim pagos As Integer = report.VfacturaspagoTableAdapter.ScalarQueryNumeroPagosPorFactura(Val(IdTextBox.Text))
                    report.PageHeight = report.PageHeight + 19 * (items - 1)
                    report.PageHeight = report.PageHeight + 19 * (pagos - 1)

                    AddHandler report.PrintingSystem.StartPrint, AddressOf ReportOnStartPrint
                    report.Print()
                    'report.ShowPreview()
                Else
                    Dim report As New rptFacturaReimpresa2

                    report.VreportefacturaimpresaTableAdapter.FillByIdFact(report.DbFireAndIceDataSet1.vreportefacturaimpresa, Val(IdTextBox.Text))
                    report.VfacturaspagoTableAdapter.FillByIdFact(report.DbFireAndIceDataSet1.vfacturaspago, Val(IdTextBox.Text))
                    report.VfacturadetalleTableAdapter.FillByIdFact(report.DbFireAndIceDataSet1.vfacturadetalle, Val(IdTextBox.Text))
                    report.SysconfigTableAdapter1.FillById(report.DbFireAndIceDataSet1.sysconfig, 1)

                    Dim items As Integer = report.VfacturadetalleTableAdapter.ScalarQueryNumeroItemsPorFactura(Val(IdTextBox.Text))
                    Dim pagos As Integer = report.VfacturaspagoTableAdapter.ScalarQueryNumeroPagosPorFactura(Val(IdTextBox.Text))
                    report.PageHeight = report.PageHeight + 19 * (items - 1)
                    report.PageHeight = report.PageHeight + 19 * (pagos - 1)

                    AddHandler report.PrintingSystem.StartPrint, AddressOf ReportOnStartPrint
                    report.Print()
                    'report.ShowPreview()
                End If
            End If
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No se pudo imprimir la factura.")
            TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, My.Resources.StringGlobal.prgAngely, "Sentencia para anular la factura en la pantalla de Historial Facturas", ex.Message, My.Resources.StringGlobal.mdlFacturacion, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
        End Try

    End Sub

    Private Sub ReportOnStartPrint(ByVal sender As Object, ByVal e As DevExpress.XtraPrinting.PrintDocumentEventArgs)
        e.PrintDocument.PrinterSettings.Copies = 1
    End Sub

    'Private Sub startPrintEventHandler(ByVal sender As Object, ByVal e As PrintDocumentEventArgs)
    '    ' Get printer settings.
    '    Dim prnSettings As PrinterSettings = e.PrintDocument.PrinterSettings
    '    ' Set the range of pages to print.
    '    'prnSettings.FromPage = 20
    '    'prnSettings.ToPage = 40
    '    ' Set the number of copies.
    '    prnSettings.Copies = 2
    '    ' Set the printer to print the document.
    '    'prnSettings.PrinterName = "MyPrinter"
    'End Sub
End Class